import 'dart:convert';
import 'dart:io';

void main() {
  String str = '{"name": "张三", "age": 20 }';

  Map<String, dynamic> map = json.decode(str); // json字符串转化map对象

  People p = People.TransJSONToClass(map); // 转化为类对象

  print(p.name);
}

class People {
  String? name;
  String? gender;

  People({this.name, this.gender});

  factory People.TransJSONToClass(Map<String, dynamic> json) {
    return People(name: json['name'], gender: json['gender']);
  }
}

fun1() {
  print('开始执行');

  getGoodList()
      .then((value) => {
            print('$value'),
          })
      .catchError((e) {
    print('$e');
  });

  print('结束执行');
}

Future<String> getGoodList() {
  return Future<String>(() {
    sleep(new Duration(seconds: 3));
    print('获取商品列表完成');
    return '商品列表';
  });
}

// 回调地狱
fun2() {
  getGoodList().then((value) {
    print('$value');
    getGoodList().then((value) {
      print('$value');
      getGoodList().then((value) => {print('$value')});
    }).catchError((e) => {print('$e')});
  }).catchError((e) => {print('$e')});
}

// 链式调用
fun3() {
  getGoodList().then((value) {
    print('$value');
    return getGoodList();
  }).then((value) {
    print('$value');
    return getGoodList();
  }).then((value) => {print('$value')});
}

// async await 语法糖
fun4() async {
  String value = await getGoodList();
  print('$value');
  String value2 = await getGoodList();
}

// 泛型的用法 函数
T fun5<T>(T value) {
  return value;
}

// 泛型的用法 class
class Person<T> {
  List<T> _originList = []; // 下划线表示私有

  totalCount() {
    return _originList.length;
  }

  getData(index) {
    return _originList[index];
  }
}
